$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
}
$(function () {
    $('#carform')
        .bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                carnumber: {
                    message: '车牌号是必需的，不能为空',
                    validators: {
                        notEmpty: {
                            message: '车牌号是必需的，不能为空'
                        },
                        stringLength: {
                            min: 7,
                            max: 7,
                            message: '车牌号固定7位'
                        },
                        regexp: {
                            regexp: /^([京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][A-Za-z][1-9DF][1-9ABCDEFGHJKLMNPQRSTUVWXYZ]\d{3}[1-9DF]|[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][\dABCDEFGHJKLNMxPQRSTUVWXYZ]{5})$/,
                            message:
                                '请输入正确的车牌'
                        }
                        /*remote: {
                            url: 'remote.php',
                            message: 'The username is not available'
                        },*/
                        // regexp: {
                        //     regexp: /^[a-zA-Z0-9_\.]+$/,
                        //     message: 'The username can only consist of alphabetical, number, dot and underscore'
                        // }
                    }
                },
                carername: {
                    validators: {
                        notEmpty: {
                            message: '请输入车主姓名'
                        }
                        ,
                        // emailAddress: {
                        //     message: 'The input is not a valid email address'
                        // }
                    }
                }
                ,
                recorddate: {
                    validators: {
                        notEmpty: {
                            message: '请选择登记日期'
                        }
                    }
                }
                ,
                licensenumber: {
                    validators: {
                        notEmpty: {
                            message: '请输入车架型号'
                        },
                        regexp:{
                            regexp:/^[A-HJ-NPR-Z\d]{17}$/,
                            message:'请输入正确的车架号'
                        }
                    }
                }
                ,
                brandmodel: {
                    validators: {
                        notEmpty: {
                            message: '请输入品牌型号'
                        }
                    }
                }
                ,

                idcard: {
                    validators: {
                        notEmpty: {
                            message: '请输入身份证号'
                        }
                        ,
                        stringLength: {
                            min: 18,
                            max:
                                18,
                            message:
                                '身份证号固定18位'
                        }
                        ,
                        regexp: {
                            regexp: /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
                            message:
                                '请输入有效身份证号'
                        }
                    }
                }
            }
        })
        .on('success.form.bv', function (e) {
            $("#userid").val(JSON.parse(sessionStorage.getItem("user")).userid);

            //将表单元素序列化成一个js对象
            let carform = $('#carform').serializeObject();
            $.ajax({
                url: 'http://127.0.0.1:8003/tsafetype/FillinVehicleiFnormation',
                type: 'post',
                contentType: 'application/json;charset=UTF-8',
                data: JSON.stringify(carform),
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        console.log(res.data);
                        location.href = "calculatorCar.html";
                    } else {
                        layer.msg(res.data);
                    }
                },
            });

        });

    if (sessionStorage.getItem("user") == null) {
        $("#center").hide();
        $("#login").show();
    } else {
        $("#login").hide();
        $("#center").show();
    }

});

